﻿using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;
using MM.Commmon.UI;
using MM.Common.Base;
using MM.Common.DBBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmFunctionPointEdit : FrmBase
    {
        public FrmFunctionPointEdit()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            base.DialogResult = DialogResult.Cancel;
            base.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            bool flag = false;
            string text = this.txtFunctionPointCode.Text;
            string paramValue = this.txtFunctionPointName.Text;
            string str3 = this.txtDescribe.Text;
            string[] commandtexts = new string[3];
            DataParamCol[] parms = new DataParamCol[3];
            if (this.isNew)
            {
                commandtexts[0] = "INSERT INTO MXHLY_SYS_FUNCTION_POINT (FUNCTION_POINT_CODE ,FUNCTION_POINT_NAME ,FORM_CODE ,DESCRIBE ,DEFAULT_PROPERTY) \r\n   VALUES (:functionpointcode ,:functionpointname ,:formcode ,:describe ,:defaultproperty)";
                parms[0] = new DataParamCol(new DataParam[] { new InputDataParam("functionpointcode", text), new InputDataParam("functionpointname", paramValue), new InputDataParam("formcode", this.functionNode.GetValue("CODE").ToString()), new InputDataParam("describe", str3), new InputDataParam("defaultproperty", this.cbxProperty.SelectedValue.ToString()) });
            }
            else
            {
                commandtexts[0] = "UPDATE MXHLY_SYS_FUNCTION_POINT SET FUNCTION_POINT_CODE = :functionpointcode ,FUNCTION_POINT_NAME = :functionpointname , DESCRIBE = :describe ,DEFAULT_PROPERTY = :defaultproperty  WHERE FUNCTION_POINT_CODE = :oldfunctionpointcode";
                parms[0] = new DataParamCol(new DataParam[] { new InputDataParam("functionpointcode", text), new InputDataParam("functionpointname", paramValue), new InputDataParam("describe", str3), new InputDataParam("defaultproperty", this.cbxProperty.SelectedValue.ToString()), new InputDataParam("oldfunctionpointcode", this.functionNode.GetValue("CODE").ToString()) });
            }
            if (!this.isNew && (this.txtFunctionPointCode.Text != this.functionNode.GetValue("CODE").ToString()))
            {
                commandtexts[1] = "UPDATE MXHLY_SYS_FUNCTION_POINT_ROLE SET FUNCTION_POINT_CODE = :newcode WHERE FUNCTION_POINT_CODE = :oldcode";
                parms[1] = new DataParamCol(new DataParam[] { new InputDataParam("newcode", text), new InputDataParam("oldcode", this.functionNode.GetValue("CODE").ToString()) });
                commandtexts[2] = "UPDATE MXHLY_SYS_FUNCTION_POINT_EMP SET FUNCTION_POINT_CODE = :newcode WHERE FUNCTION_POINT_CODE = :oldcode";
                parms[2] = new DataParamCol(new DataParam[] { new InputDataParam("newcode", text), new InputDataParam("oldcode", this.functionNode.GetValue("CODE").ToString()) });
                flag = SystemInfo.DbData.RunSql(commandtexts, parms) == 0;
            }
            else if (SystemInfo.DbData.ExecuteSql(commandtexts[0], parms[0]) >= 0)
            {
                flag = true;
            }
            if (!flag)
            {
                XtraMessageBox.Show("更新不成功！\n原因：" + SystemInfo.DbData.ErrorMessage, "出错", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                if (!this.isNew)
                {
                    this.functionNode.SetValue("CODE", text);
                    this.functionNode.SetValue("NAME", paramValue);
                    this.functionNode.SetValue("PROPERTY", this.cbxProperty.SelectedValue.ToString());
                }
                else
                {
                    TreeListNode node = this.functionNode.TreeList.AppendNode(new object[] { text, paramValue, "", this.cbxProperty.SelectedValue.ToString() }, this.functionNode);
                    node.ImageIndex = 3;
                    node.SelectImageIndex = 3;
                    this.functionNode.TreeList.FocusedNode = node;
                }
                base.DialogResult = DialogResult.OK;
                base.Close();
            }
        }

        private void FrmFunctionEdit_Shown(object sender, EventArgs e)
        {
            this.txtFunctionPointCode.Focus();
        }

        public bool SetParameter(TreeListNode node, bool isnew)
        {
            if (!base.Init())
            {
                return false;
            }
            this.functionNode = node;
            this.isNew = isnew;
            DataTable table = new DataTable();
            table.Columns.Add("PROPERTY_CODE", typeof(string));
            table.Columns.Add("PROPERTY_NAME", typeof(string));
            table.Rows.Add(new object[] { "H", "不可视" });
            table.Rows.Add(new object[] { "R", "只读" });
            table.Rows.Add(new object[] { "W", "全部" });
            this.cbxProperty.DataSource = table;
            this.cbxProperty.DisplayMember = "PROPERTY_NAME";
            this.cbxProperty.ValueMember = "PROPERTY_CODE";
            if (!isnew)
            {
                string commandtext = "SELECT FUNCTION_POINT_NAME ,FORM_CODE ,DEFAULT_PROPERTY ,DESCRIBE FROM MXHLY_SYS_FUNCTION_POINT    WHERE FUNCTION_POINT_CODE = :functionpointcode";
                DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("functionpointcode", node.GetValue("CODE").ToString()) });
                DataTable dt = new DataTable();
                SystemInfo.DbData.Retrieve(commandtext, parm, ref dt);
                this.txtFunctionPointCode.Text = node.GetValue("CODE").ToString();
                this.txtFunctionPointName.Text = dt.Rows[0]["FUNCTION_POINT_NAME"].ToString();
                this.txtDescribe.Text = dt.Rows[0]["DESCRIBE"].ToString();
                this.cbxProperty.SelectedValue = dt.Rows[0]["DEFAULT_PROPERTY"].ToString();
                this.Text = "功能点修改";
            }
            else
            {
                this.Text = "功能点新增";
                this.txtFunctionPointCode.Text = this.functionNode.GetValue("CODE").ToString() + "-";
                this.cbxProperty.SelectedValue = "W";
            }
            return true;
        }
    }
}
